package com.joke.locomotive;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;

import com.jdbc.mysql.MysqlServerConnect;
import com.joke.util.Const;
import com.joke.util.DateUtil;

public class ImportImage {
	
	private static String date = DateUtil.formatString(new Date(), "yyyyMM");
	private static String datePath = DateUtil.formatString(new Date(), "yyyyMMdd");
	private static String Y = DateUtil.formatString(new Date(), "yyyy");
	private static String M = DateUtil.formatString(new Date(), "MM");
	private static String D = DateUtil.formatString(new Date(), "dd");
	private static String ymd = Y+"/"+M+"/"+D;
	static String writeImagePath=Const.fabu+date+Const.localImage;
	
	@SuppressWarnings("hiding")
	public static void readImage(String[] filePaths) {
		Scanner scanner;
		try {
			Map<String, String> hotMap = new HashMap<String, String>();

			for (int i = 0; i < filePaths.length; i++) {
				
				String line = "";
				String content = "";
				String path = "";
				int l = 0;
				
				FileInputStream fis = new FileInputStream(new File(filePaths[i]));
				scanner = new Scanner(fis);
				//从都一行开始读。第一行是内容，第二行是图片（如果没有则为空），第三行是空行；以此类推
				while (scanner.hasNextLine()) {
					
					line = scanner.nextLine();
					
					if (l == 0) {
						if (!"".equals(line.trim()) &&line.trim().length()<=200  && line.indexOf("匿") == -1
								&& line.trim().indexOf("糗百") == -1 && line.trim().indexOf("??????") == -1
								&& line.trim().indexOf("捧腹") == -1&& line.trim().indexOf("查看全文") == -1) {
							content = line.trim().replace("", "").replace("🙃", "").replace("🙏", "")
									.replace("🐻", "").replace("😂", "").replace("$$", "")
									.replaceAll("[\\ud800\\udc00-\\udbff\\udfff\\ud800-\\udfff]", "");
//							System.out.println(content);
						}
						l++;
					} else if (l == 1) {
						if(line.trim().indexOf(Const.xiajiongPre)!=-1) {
							path = line.trim().replace(Const.picPre+datePath+Const.o, "")
									.replace(Const.picPre+ymd+Const.o,"");
//							System.out.println(path);
						}else {
							path = "";
						}
						l++;
					} else if (l == 2) {
						if(!"".equals(content.trim())) {
							hotMap.put(content, path);
						}
						content = "";
						path = "";
						l = 0;
					} 

				}
				
			}
			System.out.println("全部读取完毕，共"+hotMap.size()+"条趣图");
			
			//保存入库趣图
			writeImage(hotMap);
			//保存数据库  本地和服务器
	    	MysqlServerConnect.insertImageLoco(hotMap);
			
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
			
	}
	
	/**
	 * 将入库的文本趣图做个备份,写入Txt文件
	 * @param map
	 */
	public static void writeImage(Map<String, String> hotMap) {
		
		BufferedWriter out = null;
		try {
			out = new BufferedWriter(new FileWriter(writeImagePath, true));//追加形式
			for (String str : hotMap.keySet()) {
				if(!"".equals(str.trim())) {
					out.write(str+"\r\n"); 
					out.write(hotMap.get(str)+"\r\n");   
				}
			}
			System.out.println("写入txt完毕");
		} catch (IOException e) {
			e.printStackTrace();
		}  finally {
			try {
				out.flush();
			} catch (IOException e) {
				e.printStackTrace();
			}    
			try {
				out.close();
			} catch (IOException e) {
				e.printStackTrace();
			}  
		}
	}
	
	
	public static void main(String[] args) {
		String date = DateUtil.formatString(new Date(), DateUtil.YMD);
		String [] filePaths = new String[2];
		filePaths[0] = "D:\\LocoDown\\fabu\\"+date+"neihanImages.txt";
		filePaths[1] = "D:\\LocoDown\\fabu\\"+date+"pengfuGifImages.txt";
		readImage(filePaths);
	}

}
